package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.PurchaseOrderDetailsMapper;
import com.guigu.youxuan.mapper.PurchaseOrderMapper;
import com.guigu.youxuan.mapper.SupplierInfoMapper;
import com.guigu.youxuan.pojo.OrderInfo;
import com.guigu.youxuan.pojo.PurchaseOrder;
import com.guigu.youxuan.pojo.PurchaseOrderDetails;
import com.guigu.youxuan.pojo.SupplierInfo;
import com.guigu.youxuan.pojo.dto.PurchaseAndDetailDto;
import com.guigu.youxuan.pojo.dto.PurchaseReviewDto;
import com.guigu.youxuan.pojo.dto.SupplierGoodsDto;
import com.guigu.youxuan.service.IPurchaseOrderDetailsService;
import com.guigu.youxuan.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements IPurchaseOrderService {
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private IPurchaseOrderDetailsService purchaseOrderDetailsService;
    @Autowired
    private PurchaseOrderDetailsMapper purchaseOrderDetailsMapper;
    /**
     * 供应商订单分页查询采购订单信息
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<PurchaseOrder> SupplierOrderListPage(int sysUsersId, int currentPage, int pageSize) {
        int supplierId=supplierInfoMapper.listSupplierId(sysUsersId);
        List<PurchaseOrder> goodsInfos=purchaseOrderMapper.list(supplierId);
        PageHelper.startPage(currentPage,pageSize);
        PageInfo<PurchaseOrder> pageInfo=new PageInfo<>(goodsInfos);
        return pageInfo;
    }

    @Override
    public String getMaxDesignId() {
        PurchaseOrder purchaseOrder = purchaseOrderMapper.getMaxDesignId();
        if(purchaseOrder == null){
            return null;
        }
        // 获取设计单编号
        String designId = purchaseOrder.getPurchaseOrderNumber();
        return designId;
    }

    @Transactional
    @Override
    public void createPurchase(PurchaseOrder purchaseOrder, List<SupplierGoodsDto> supplierGoodsList) {
        this.save(purchaseOrder);
        for(SupplierGoodsDto dto : supplierGoodsList) {
            PurchaseOrderDetails purchaseOrderDetails = new PurchaseOrderDetails();
            purchaseOrderDetails.setPurchaseId(purchaseOrder.getPurchaseId());
            purchaseOrderDetails.setCount(dto.getAmount());
            purchaseOrderDetails.setPrice(dto.getPrice());
            purchaseOrderDetails.setSubtotal(dto.getPrice()*dto.getAmount());
            purchaseOrderDetails.setGoodsId(Integer.parseInt(dto.getGoodsId()));
            purchaseOrderDetailsService.save(purchaseOrderDetails);
        }
    }

    @Override
    public PageUtils<PurchaseReviewDto> queryPurchaseReview(SupplierInfo supplierInfo, int currentPage, int pageSize) {

        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("register_time desc");

        List<PurchaseReviewDto> list = purchaseOrderMapper.queryPurchaseReview(supplierInfo);

        PageInfo<PurchaseReviewDto> pageInfo = new PageInfo<>(list);
        PageUtils<PurchaseReviewDto> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;

    }

    @Override
    public PurchaseAndDetailDto queryPurchaseAndDetail(String purchaseId) {
        PurchaseAndDetailDto dto = new PurchaseAndDetailDto();
        QueryWrapper <PurchaseOrder> w = new QueryWrapper<>();
        w.eq("purchase_id",purchaseId);
        dto.setPurchaseOrder(this.getOne(w));
        dto.setPurchaseOrderDetails(purchaseOrderDetailsMapper.list(purchaseId));
        return dto;
    }

    @Override
    public void purchaseReviewOk(PurchaseOrder purchaseOrder) {
        purchaseOrder.setCheckTag("S001-3");
        purchaseOrder.setConveyTag("P001-2");
        purchaseOrder.setCheckTime(new Date());
        this.updateById(purchaseOrder);
    }

    @Override
    public PageUtils<PurchaseReviewDto> queryPurchaseReviewOk(SupplierInfo supplierInfo, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("register_time desc");

        List<PurchaseReviewDto> list = purchaseOrderMapper.queryPurchaseReviewOk(supplierInfo);

        PageInfo<PurchaseReviewDto> pageInfo = new PageInfo<>(list);
        PageUtils<PurchaseReviewDto> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }
}
